package ar.com.ford.it.fullEconomics.persistence.dao.query;

import java.sql.Timestamp;
import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import org.apache.commons.lang.StringUtils;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.struct.RequestFindStruct;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class RequestItemsFindQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder,
                                       Object[] parameters) {
        Expression exp = null;
        RequestFindStruct findStruct= (RequestFindStruct) parameters[0];
        
        Teesg01Plant plant = findStruct.getPlant();
        String supplierClass = findStruct.getSupplierClass();
        String supplierCode = findStruct.getSupplierCode();
        String supplierName = findStruct.getSupplierName();
        Integer commodity = findStruct.getCommodity();
        String partClass = findStruct.getPartClass();
        String prefix = findStruct.getPrefix();
        String base = findStruct.getBase();
        String suffix = findStruct.getSuffix();
        String dateField = findStruct.getDateField();
        Timestamp dateFrom = findStruct.getDateFrom();
        Timestamp dateTo = findStruct.getDateTo();
        String userField = findStruct.getUserField();
        String userCode = findStruct.getUserCode();
        String status = findStruct.getStatus();
        
        exp = builder.get("eesr02PlantCodeC").equal(plant);
        if (supplierClass != null && !supplierClass.equals("-1")) {
            exp = exp.and(builder.get("teesr01Request").get("teess01Supplier").get("eess01SupplierClassCodeC").equal(
                    supplierClass));
        }
        if (supplierCode != null) {
            supplierCode += "%";
            exp = exp.and(builder.get("teesr01Request").get("teess01Supplier").get("eess01SupplierCodeC").like(
                    supplierCode));
        }
        if(supplierName != null){
            supplierName += "%";
            exp = exp.and(builder.get("teesr01Request").get("teess01Supplier").get("eess01SupplierNameN").like(
                    supplierName));
        }
        
        if(dateField.equals("R")){
            exp = exp.and(builder.get("teesr01Request").get("eesr01RequestIssueDateY").between(dateFrom, dateTo));
        }else if(dateField.equals("C")){
            exp = exp.and(builder.get("teesr01Request").get("eesr01RequestClosureS").between(dateFrom, dateTo));
        }
        if(userField.equals("CR")){
            exp = exp.and(builder.get("teesr01Request").get("eesr01CreateUserC").equal(userCode));
        }else if(userField.equals("CI")){
            exp = exp.and(builder.get("teesr01Request").get("eesr01RequestClosureUserC").equal(userCode));
        }
        if(!status.equals("-1")){
            exp = exp.and(builder.get("teesr01Request").get("eesr01RequestStatusCodeC").equal(status));
        }
        
        if (partClass != null && !partClass.equals("-1")) {
            exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get("eesp01PartClassCodeC").equal(
                    partClass));
        }
        if (prefix != null && !StringUtils.isBlank(prefix)) {
            prefix += "%";
            exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get("eesp01PartPrefixCodeC").like(
                    prefix));
        }
        if (base != null && !StringUtils.isBlank(base)) {
            base += "%";
            exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get("eesp01PartBaseCodeC").like(
                    base));
        }
        if (suffix != null && !StringUtils.isBlank(suffix)) {
            suffix += "%";
            exp = exp.and(builder.get("teesp04Supply").get("teesp01Part").get("eesp01PartSuffixCodeC").like(
                    suffix));
        }
        if(commodity != -1){
            exp = exp.and(builder.get("teesp02Commodity").get("eesp02CommodityNumberK").equal(
                    commodity));
        }
        
        return exp;
    }

}
